#! /bin/bash

SD=$1
PARTITION_MODE=$2
VIRTUAL_MACHINE=$3
HD_NTFS_SIZE=$4

source $(dirname $(dirname $(realpath $0)))/config/echo_config || exit 1
source $CONFIG_DIR/env_config || exit 1

function umount
{
    echo_start "umount"
    if ! $SCRIPT_DIR/umount.sh $SD $PARTITION_MODE; then
        echo_err "umount"
        exit 1
    fi
    echo_finish "umount"
}

umount

if ! $SCRIPT_DIR/partition.sh $SD $PARTITION_MODE $HD_NTFS_SIZE; then
    echo_err "partition"
    umount
    exit 1
fi

echo_start "make mirrors"
if ! {
    cp $RESOURCE_DIR/archlinuxcn-mirrorlist /etc/pacman.d/archlinuxcn-mirrorlist &&
    reflector --verbose --country China -p http --sort rate --save /etc/pacman.d/mirrorlist &&
    sed -i 's/^#ParallelDownloads/ParallelDownloads/g' /etc/pacman.conf &&
    pacman -Syy &&
    pacman -S pacman-contrib --noconfirm
}; then
    echo_err "make mirrors"
    exit 1
fi
echo_finish "make mirrors"

echo_start "config mkinitcpio"
case $PARTITION_MODE in
    USB_MBR | USB_GPT | USB_Hybrid | USB_Hybrid_HD )
        if ! sed -i 's/^HOOKS=(\(.*\)autodetect \(.*\) block\(.*\))$/HOOKS=(\1block autodetect \2\3)/g; s/^HOOKS=(\(.*\)autodetect \(.*\) keyboard\(.*\))$/HOOKS=(\1keyboard autodetect \2\3)/g' /etc/mkinitcpio.conf; then
            echo_err "change /etc/mkinitcpio.conf"
            exit 1
        fi
        ;;
esac
echo_finish "config mkinitcpio"

echo_start "pacstrap"
INSTALL_PACKAGES=(base linux linux-firmware base-devel grub)
case $PARTITION_MODE in
    MBR | GPT | BtrFS | USB_Hybrid_HD )
        INSTALL_PACKAGES+=(btrfs-progs)
        ;;
esac
case $PARTITION_MODE in
    USB_GPT | USB_Hybrid | GPT | USB_Hybrid_HD )
        INSTALL_PACKAGES+=(efibootmgr)
        ;;
esac
if ! yes ' ' | pacstrap -i $ROOT ${INSTALL_PACKAGES[@]}; then
    echo_err "pacstrap"
    umount
    exit 1
fi
echo_finish "pacstrap"

echo_start "genfstab"
if ! genfstab -U -p $ROOT | sed -e 's#^\(\S\+\)\(\s\+\)/var/lib\(\s\+\)btrfs\(\s\+\)\(\([^,]\+,\)\{4\}\)\([^,]\+\)\S\+\(.*\)$#\1\2/run/f\3btrfs\4\5\7\8\n/run/f/active/ROOT/var/lib\t\t\t/var/lib\tnone\t\tbind 0 0#g' >> $ROOT/etc/fstab; then
    echo_err "genfstab"
    umount
    exit 1
fi
echo_finish "genfstab"

echo_start "rsync"
if [ -f $ROOT_DIR/fonts.tar.xz ]; then
    tar -C $RESOURCE_DIR -xpf $ROOT_DIR/fonts.tar.xz > /dev/null 2>&1
else
    mkdir -p $RESOURCE_DIR/fonts
fi
if [ -f $ROOT_DIR/user.tar.xz ]; then
    tar -C $RESOURCE_DIR -xpf $ROOT_DIR/user.tar.xz > /dev/null 2>&1
fi
if ! {
    rsync -avP /etc/pacman.d/mirrorlist $ROOT/etc/pacman.d/mirrorlist &&
    rsync -avP /etc/pacman.d/archlinuxcn-mirrorlist $ROOT/etc/pacman.d/archlinuxcn-mirrorlist &&
    sed -i 's/^#ParallelDownloads/ParallelDownloads/g' $ROOT/etc/pacman.conf &&
    cat << EOF >> $ROOT/etc/pacman.conf &&
[archlinuxcn]
SigLevel = Optional TrustAll
Include = /etc/pacman.d/archlinuxcn-mirrorlist
EOF
    rsync -avP $SCRIPT_DIR/root_install.sh $ROOT/root/root_install.sh &&
    mkdir -p $ROOT/root/tmp &&
    rsync -avP $SCRIPT_DIR/mew_install.sh $ROOT/root/tmp/mew_install.sh &&
    rsync -avP $RESOURCE_DIR/prev_system/. $ROOT &&
    rsync -avP $RESOURCE_DIR/system $ROOT/root/tmp &&
    rsync -avP $RESOURCE_DIR/user $ROOT/root/tmp &&
    rsync -avP $RESOURCE_DIR/fonts $ROOT/root/tmp &&
    rsync -avP $CONFIG_DIR/echo_config $ROOT/root/tmp/echo_config &&
    rsync -avP $CONFIG_DIR/env_config $ROOT/root/tmp/env_config
}; then
    echo_err "rsync"
    umount
    exit 1
fi
echo_finish "rsync"

if ! arch-chroot $ROOT /bin/bash /root/root_install.sh $SD $PARTITION_MODE $VIRTUAL_MACHINE; then
    echo_err "arch-chroot"
    umount
    exit 1
fi

if ! {
    rm $ROOT/root/root_install.sh &&
    rm -rf $ROOT/root/tmp
}; then
    echo_err "rm"
    umount
    exit 1
fi

umount

exit 0
